/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg1;

import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JPanel;

/**
 * Objects of this class represent rooms in the domain model.
 * @author Nick
 */
public class Room extends JPanel{
    //The unique ID of a room
    private int id;
    //state of the room (change layout or not)
    private boolean inLayoutMode;
    //Used for displaying purposes in the template view
    private int numberOfBedsBeforeDisable = 0;
    //whether or not the room is enabled
    private boolean enabled = true;
    //The list of beds in the room
    private ArrayList<Bed> beds = new ArrayList<Bed>();
    //constructor of a room
    public Room(int id, boolean inLayoutMode) {
        this.inLayoutMode = inLayoutMode;
        this.id = id;
        this.addMouseListener(new MouseHandler1());
    }

    /**
     * @return the id of the room
     */
    public int getId() {
        return id;
    }
    
    /**
     * @return whether or not the room is in layout mode
     */
    public boolean isInLayoutMode() {
        return inLayoutMode;
    }
    
    /**
     * sets the state of the room
     * @param b 
     */
    public void setLayoutMode(boolean b) {
        inLayoutMode = b;
    }

    /**
     * @return the beds of the room
     */
    public ArrayList<Bed> getBeds() {
        return beds;
    }
    
    // Whether or not a point overlaps the current panel.
    public boolean overlaps(Point p) {
        if(this.contains(p)) {
            return true;
        }
        return false;
    }
    
    /**
     * Allows the user to edit the layout when in the correct state
     */
    private class MouseHandler1 extends MouseAdapter {
      public void mouseClicked(MouseEvent event) {
         Point p = (event.getPoint());
             if (overlaps(p) && inLayoutMode) {
                 EditRoomState roomState = new EditRoomState(Room.this);
                 roomState.setLocationRelativeTo(null);
                 roomState.setVisible(true);
             }
      }
    }
    
    /**
     * sets the number of visible beds in the room
     * @param numberOfBeds 
     */
    public void setNumberOfBeds(int numberOfBeds) {
        if(inLayoutMode) {
            if(numberOfBeds == 1) {
                setOneBeds();
            }
            if(numberOfBeds == 2) {
                setTwoBeds();
            }
            if(numberOfBeds == 3) {
                setThreeBeds();
            }
            if(numberOfBeds == 4) {
                setFourBeds();
            }
        }
    }
    
    //Helper method, sets the availability and visibilty of beds according to 
    //One being visible
    private void setOneBeds() {
        beds.get(0).setAvailable(true);
        beds.get(0).setVisible(true);
        beds.get(1).setAvailable(false);
        beds.get(1).setVisible(false);
        beds.get(2).setAvailable(false);
        beds.get(2).setVisible(false);
        beds.get(3).setAvailable(false);
        beds.get(3).setVisible(false);
    }
    
    //Helper method, sets the availability and visibilty of beds according to 
    //Two being visible
    private void setTwoBeds() {
        beds.get(0).setAvailable(true);
        beds.get(0).setVisible(true);
        beds.get(1).setAvailable(false);
        beds.get(1).setVisible(false);
        beds.get(2).setAvailable(false);
        beds.get(2).setVisible(false);
        beds.get(3).setAvailable(true);
        beds.get(3).setVisible(true);
    }
    
    //Helper method, sets the availability and visibilty of beds according to 
    //Three being visible
    private void setThreeBeds() {
        beds.get(0).setAvailable(true);
        beds.get(0).setVisible(true);
        beds.get(1).setAvailable(true);
        beds.get(1).setVisible(true);
        beds.get(2).setAvailable(true);
        beds.get(2).setVisible(true);
        beds.get(3).setAvailable(false);
        beds.get(3).setVisible(false);
    }
    
    //Helper method, sets the availability and visibilty of bedss according to 
    //Four being visible
    private void setFourBeds() {
        beds.get(0).setAvailable(true);
        beds.get(0).setVisible(true);
        beds.get(1).setAvailable(true);
        beds.get(1).setVisible(true);
        beds.get(2).setAvailable(true);
        beds.get(2).setVisible(true);
        beds.get(3).setAvailable(true);
        beds.get(3).setVisible(true);
    }
    
    /**
     * sets the criticality of the room
     * @param critical 
     */
    public void setCritical(boolean critical) {
        for(Bed bed : beds) {
            bed.setCriticalCare(critical);
        }
    }
    
    /**
     * disables the room
     */
    public void setDisabled() {
        numberOfBedsBeforeDisable = getNumberOfVisibleBeds();
        for(Bed bed : beds) {
            bed.setVisible(false);
        }
        enabled = false;
    }
    
    /**
     * enables the room
     */
    public void setEnabled() {
        setNumberOfBeds(numberOfBedsBeforeDisable);
        enabled = true;
    }
    
    /**
     * gets the criticality of the room
     * @return 
     */
    public boolean isCriticalCare() {
        if(beds.get(0).isCriticalCare()) {
            return true;
        }
        return false;
    }
    
    /**
     * gets the number of visible beds in the room
     * @return 
     */
    public int getNumberOfVisibleBeds() {
        if(enabled) {
            int counter = 0;
            for(Bed bed : beds) {
                if(bed.isVisible()) {
                    counter++;
                }
            }
            return counter;
        }
        else
            return numberOfBedsBeforeDisable;
    }
    
    /**
     * whether or not the room is enabled
     * @return 
     */
    public boolean isEnabled() {
        return enabled;
    }
    
    /**
     * gets a string with all the information necessary to save the room.
     * @return 
     */
    public String getSaveString() {
        String saveString = "";
        saveString += "Id:" + id + ",";
        saveString += "Enabled:" + enabled + ",";
        saveString += "InLayoutMode:" + inLayoutMode;
        return saveString;
    }
}
